#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides:          telemetry
# Required-Start:    networking
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Open telemetry socket service
# Description:       Wrapper for starting / stopping GoldVIP telemetry service
#                    Runs when domain 0 boots / shuts down on systems using init.
### END INIT INFO

SERVER_PATH=/home/root/cloud-gw/telemetry-collector/dds_telemetry_pub.py
LOG_PATH="/var/log/collector_server.log"

case "$1" in
start)
        echo "Starting telemetry service"
        python3 ${SERVER_PATH} > ${LOG_PATH} 2>&1 &
        ;;
stop)
        echo "Stopping telemetry service"
        kill -9 $(ps aux | grep '[d]ds_telemetry_pub.py' | awk '{print $2}') 2> /dev/null
        ;;
restart)
        echo "Restarting telemetry service"
        kill -9 $(ps aux | grep '[d]ds_telemetry_pub.py' | awk '{print $2}') 2> /dev/null
        python3 ${SERVER_PATH} > ${LOG_PATH} 2>&1 &
        ;;
*)
        echo "Usage: /etc/init.d/telemetry {start|stop|restart}"
        exit 1
        ;;
esac

exit 0